package com.gonsin.gmessage.logic.proxy;

import com.gonsin.gmessage.annotations.GArgsBody;
import com.gonsin.gmessage.logic.utils.*;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtilsBean;

import java.util.Map;

/**
 * 被@GArgs批注的参数的描述
 */
public class ArgsBodyDescribeModel implements IArgsPush{

    /**
     * 所在参数位置
     */
    private int index;

    /**
     * 参数类型
     */
    private Class<?> rowType;

    public ArgsBodyDescribeModel(GArgsBody body, int index, Class<?> rowType) {
        this.index = index;
        this.rowType = rowType;

    }

    @SneakyThrows
    @Override
    public void put(Map<String, Object> args, Object param) {
        if(param instanceof Map){
            args.putAll((Map<? extends String, ?>) param);
            return;
        }

        BeanUtilsBean utils = BeanUtilsBean.getInstance();
        utils.getConvertUtils().register(new MyDateConverter(), java.util.Date.class);
        utils.getConvertUtils().register(new MyBooleanConverter(), Boolean.class);
        utils.getConvertUtils().register(new MyIntegerConverter(), Integer.class);
        utils.getConvertUtils().register(new MyLongConverter(), Long.class);
        utils.getConvertUtils().register(new MyShortConverter(), Short.class);
        utils.getConvertUtils().register(new MyByteConverter(), Byte.class);
        utils.getConvertUtils().register(new MyDoubleConverter(), Double.class);
        utils.getConvertUtils().register(new MyFloatConverter(), Float.class);
        utils.getConvertUtils().register(new MyCharConverter(), Character.class);
        utils.populate(param, args);
    }
}
